package com.masterclient.app.modules.income;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.eoner.baselib.utils.toast.ToastUtil;
import com.eoner.baselibrary.utils.Utils;
import com.eoner.managerlibrary.router.AroutePath;
import com.masterclient.app.BaseFragment;
import com.masterclient.app.R;
import com.masterclient.app.modules.income.contract.WithdrawDetailsContract;
import com.masterclient.app.modules.income.contract.WithdrawDetailsPresenter;
import com.masterclient.app.modules.income.model.UnfreezeDetailsBean;
import com.masterclient.app.modules.income.model.WithdrawDetailsBean;

import java.util.Objects;

import butterknife.BindView;
import butterknife.OnClick;
import pl.droidsonroids.gif.GifImageView;

@Route(path = AroutePath.WITHDRAW_DETAILS_FRAGMENT)
public class WithdrawDetailsFragment extends BaseFragment<WithdrawDetailsPresenter> implements WithdrawDetailsContract.View {

    public static String PAGE_TYPE = "page_type";
    public static String WITHDRAW_DETAILS = "withdraw_details";
    public static String UNFREEZE_DETAILS = "unfreeze_details";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_state)
    ImageView ivState;
    @BindView(R.id.iv_gif_state)
    GifImageView ivGifState;
    @BindView(R.id.view_unfreeze)
    View viewUnfreeze;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.view_withdraw)
    View viewWithdraw;
    @BindView(R.id.tv_withdraw_title)
    TextView tvWithdrawTitle;
    @BindView(R.id.tv_withdraw_account)
    TextView tvWithdrawAccount;
    @BindView(R.id.tv_withdraw_desc)
    TextView tvWithdrawDesc;
    @BindView(R.id.view_bottom)
    LinearLayout viewBottom;
    @Autowired(name = "page_type")
    String pageType;
    @Autowired(name = "frozen_id")
    String frozenId;
    @Autowired(name = "frozen_status")
    String frozenStatus;
    @Autowired(name = "withdraw_id")
    String withdrawId;

    public static WithdrawDetailsFragment newInstance(String pageType) {
        WithdrawDetailsFragment withdrawDetailsFragment = new WithdrawDetailsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(PAGE_TYPE, pageType);
        withdrawDetailsFragment.setArguments(bundle);
        return withdrawDetailsFragment;
    }

    @Override
    protected View getRootView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_withdraw_details, null);
    }

    @Override
    protected void initOnlyOnce() {
        super.initOnlyOnce();

        ARouter.getInstance().inject(this);

        if (UNFREEZE_DETAILS.equals(pageType)) {
            if ("3".equals(frozenStatus)) {
                tvTitle.setText("冻结");
                ivState.setVisibility(View.VISIBLE);
                ivState.setImageResource(R.mipmap.icon_freeze);
            } else {
                tvTitle.setText("解冻");
                ivState.setVisibility(View.VISIBLE);
                ivState.setImageResource(R.mipmap.icon_unfreeze);
            }
            mPresenter.getUnfreezeDetails(frozenId);
        } else {
            tvTitle.setText("提现详情");
            mPresenter.getWithdrawDetails(withdrawId);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @OnClick({R.id.back})
    void onClick(View view) {
        if (view.getId() == R.id.back) {
            Objects.requireNonNull(getActivity()).finish();
        }
    }

    @Override
    public void showUnfreezeData(UnfreezeDetailsBean dataBean) {
        if (UNFREEZE_DETAILS.equals(pageType) && dataBean != null) {
            viewUnfreeze.setVisibility(View.VISIBLE);
            ivState.setVisibility(View.VISIBLE);
            ivGifState.setVisibility(View.GONE);
            viewWithdraw.setVisibility(View.GONE);

            tvName.setText(dataBean.frozenTitle);
            //底部描述信息
            if (dataBean.contentList != null && dataBean.contentList.size() > 0) {
                viewBottom.removeAllViews();
                for (int i = 0; i < dataBean.contentList.size(); i++) {
                    View view = View.inflate(mContext, R.layout.item_withdraw_info, null);
                    TextView tvLeftTxt = view.findViewById(R.id.tv_left_txt);
                    TextView tvRightTxt = view.findViewById(R.id.tv_right_txt);
                    TextView tvCopy = view.findViewById(R.id.tv_copy);

                    tvLeftTxt.setText(dataBean.contentList.get(i).title);
                    tvRightTxt.setText(dataBean.contentList.get(i).value);
                    tvCopy.setVisibility(dataBean.contentList.get(i).copyable ? View.VISIBLE : View.GONE);
                    if (dataBean.contentList.get(i).copyable && !TextUtils.isEmpty(dataBean.contentList.get(i).value)) {
                        int finalI = i;
                        tvCopy.setOnClickListener(v -> copyTxt(dataBean.contentList.get(finalI).value));
                    }
                    viewBottom.addView(view);
                }
                viewBottom.setVisibility(View.VISIBLE);
            } else {
                viewBottom.setVisibility(View.GONE);
            }
        }
    }

    private ClipboardManager cm;

    private void copyTxt(String content) {
        if (cm == null) {
            cm = (ClipboardManager) Objects.requireNonNull(getActivity()).getSystemService(Context.CLIPBOARD_SERVICE);
        }
        cm.setPrimaryClip(ClipData.newPlainText(null, content));
        showToast("已复制到粘贴板");
    }

    @Override
    public void showWithdrawData(WithdrawDetailsBean dataBean) {
        if (WITHDRAW_DETAILS.equals(pageType) && dataBean != null) {
            viewWithdraw.setVisibility(View.VISIBLE);
            viewUnfreeze.setVisibility(View.GONE);
            if ("3".equals(dataBean.status)) {
                //提现处理中
                ivGifState.setVisibility(View.VISIBLE);
                ivState.setVisibility(View.GONE);
            } else {
                ivGifState.setVisibility(View.GONE);
                ivState.setVisibility(View.VISIBLE);
                if ("1".equals(dataBean.status)) {
                    //提现成功
                    ivState.setImageResource(R.mipmap.icon_withdraw_success);
                } else {
                    //提现失败
                    ivState.setImageResource(R.mipmap.icon_withdraw_fail);
                }
            }
            tvWithdrawTitle.setText(dataBean.title);
            tvWithdrawAccount.setText(dataBean.amount);
            Utils.setDin(tvWithdrawAccount, getActivity());
            tvWithdrawDesc.setText(dataBean.withdrawStatus);

            //提现进度
            initProgressInfo(dataBean);
            //底部描述信息
            initBottomInfo(dataBean);
        }
    }

    private void initProgressInfo(WithdrawDetailsBean dataBean) {
        if (dataBean.progress != null && dataBean.progress.detail != null && dataBean.progress.detail.size() > 0) {
            View viewProgress = View.inflate(mContext, R.layout.item_withdraw_progress_info, null);
            LinearLayout viewProgressInfo = viewProgress.findViewById(R.id.view_progress_info);
            TextView tvLeftTxt = viewProgress.findViewById(R.id.tv_left_txt);
            tvLeftTxt.setText(dataBean.progress.title);
            for (int i = 0; i < dataBean.progress.detail.size(); i++) {
                View view = View.inflate(mContext, R.layout.item_withdrow_progress, null);
                ImageView ivProgress = view.findViewById(R.id.iv_progress);
                TextView tvProgressName = view.findViewById(R.id.tv_progress_name);
                TextView ivProgressTime = view.findViewById(R.id.iv_progress_time);
                TextView tvProgressDes = view.findViewById(R.id.tv_progress_des);
                View viewProgressLineTop = view.findViewById(R.id.view_progress_line_top);
                View viewProgressLineHead = view.findViewById(R.id.view_progress_line_head);
                View viewProgressLineBottom = view.findViewById(R.id.view_progress_line_bottom);

                //进度图标：2 未选中(蓝色点点点) 3 红色× 4 处理中 5待处理（ 灰色） 6 蓝色对号（成功）
                if ("2".equals(dataBean.progress.detail.get(i).status)) {
                    ivProgress.setImageResource(R.drawable.shape_circle_07c160);
                    tvProgressName.setTextColor(Color.parseColor("#C2C5CC"));
                    ivProgressTime.setTextColor(Color.parseColor("#C2C5CC"));

                    viewProgressLineTop.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_07c160);
                    viewProgressLineHead.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_07c160);
                    viewProgressLineBottom.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_07c160);
                } else if ("3".equals(dataBean.progress.detail.get(i).status)) {
                    ivProgress.setImageResource(R.mipmap.icon_apply_withdraw_fail);
                    tvProgressName.setTextColor(Color.parseColor("#323233"));
                    ivProgressTime.setTextColor(Color.parseColor("#969799"));

                    viewProgressLineTop.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_07c160);
                    viewProgressLineHead.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_07c160);
                    viewProgressLineBottom.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_dcdee0);
                } else if ("4".equals(dataBean.progress.detail.get(i).status)) {
                    ivProgress.setImageResource(R.mipmap.icon_apply_withdraw_doing);
                    tvProgressName.setTextColor(Color.parseColor("#323233"));
                    ivProgressTime.setTextColor(Color.parseColor("#969799"));

                    viewProgressLineTop.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_07c160);
                    viewProgressLineHead.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_07c160);
                    viewProgressLineBottom.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_dcdee0);
                } else if ("5".equals(dataBean.progress.detail.get(i).status))  {
                    ivProgress.setImageResource(R.drawable.shape_circle_dcdee0);
                    tvProgressName.setTextColor(Color.parseColor("#C2C5CC"));
                    ivProgressTime.setTextColor(Color.parseColor("#C2C5CC"));

                    viewProgressLineTop.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_dcdee0);
                    viewProgressLineHead.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_dcdee0);
                    viewProgressLineBottom.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_dcdee0);
                }else if ("6".equals(dataBean.progress.detail.get(i).status)) {
                    ivProgress.setImageResource(R.mipmap.icon_apply_withdraw_success);
                    tvProgressName.setTextColor(Color.parseColor("#323233"));
                    ivProgressTime.setTextColor(Color.parseColor("#969799"));

                    viewProgressLineTop.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_07c160);
                    viewProgressLineHead.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_07c160);
                    viewProgressLineBottom.setBackgroundResource(R.drawable.bg_vertical_dotted_line_of_07c160);
                }

                if (0 == i) {
                    viewProgressLineHead.setVisibility(View.INVISIBLE);
                    viewProgressLineTop.setVisibility(View.INVISIBLE);
                    viewProgressLineBottom.setVisibility(View.VISIBLE);
                } else {
                    if (dataBean.progress.detail.size() - 1 == i) {
                        viewProgressLineBottom.setVisibility(View.INVISIBLE);
                    } else {
                        viewProgressLineBottom.setVisibility(View.VISIBLE);
                    }
                    viewProgressLineTop.setVisibility(View.VISIBLE);
                    viewProgressLineHead.setVisibility(View.VISIBLE);
                }

                if (!TextUtils.isEmpty(dataBean.progress.detail.get(i).title)) {
                    tvProgressName.setVisibility(View.VISIBLE);
                    tvProgressName.setText(dataBean.progress.detail.get(i).title);
                } else {
                    tvProgressName.setVisibility(View.GONE);
                }
                if (!TextUtils.isEmpty(dataBean.progress.detail.get(i).date)) {
                    ivProgressTime.setVisibility(View.VISIBLE);
                    ivProgressTime.setText(dataBean.progress.detail.get(i).date);
                } else {
                    ivProgressTime.setVisibility(View.GONE);
                }
                if (!TextUtils.isEmpty(dataBean.progress.detail.get(i).desc)) {
                    tvProgressDes.setVisibility(View.VISIBLE);
                    tvProgressDes.setText(dataBean.progress.detail.get(i).desc);
                } else {
                    tvProgressDes.setVisibility(View.GONE);
                }
                viewProgressInfo.addView(view);
            }
            viewBottom.addView(viewProgress);
            viewBottom.setVisibility(View.VISIBLE);
        } else {
            viewBottom.setVisibility(View.GONE);
        }
    }

    private void initBottomInfo(WithdrawDetailsBean dataBean) {
        if (dataBean.contentList != null && dataBean.contentList.size() > 0) {
            for (int i = 0; i < dataBean.contentList.size(); i++) {
                View view = View.inflate(mContext, R.layout.item_withdraw_info, null);
                TextView tvLeftTxt = view.findViewById(R.id.tv_left_txt);
                TextView tvRightTxt = view.findViewById(R.id.tv_right_txt);

                tvLeftTxt.setText(dataBean.contentList.get(i).title);
                tvRightTxt.setText(dataBean.contentList.get(i).value);
                viewBottom.addView(view);
            }
            viewBottom.setVisibility(View.VISIBLE);
        } else {
            viewBottom.setVisibility(View.GONE);
        }
    }

    @Override
    public void showDataFailed(String msg) {
        ToastUtil.showToast(msg);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
